use super::raw::serial::*;
use crate::prelude::*;
use crate::stdlib::lock::SpinLock;
use core::fmt::Result;

static mut LOCK: SpinLock = SpinLock::new();

pub struct Serial {
    port: SerialPort,
}

impl Write for Serial {
    fn write_str(&mut self, s: &str) -> Result {
        unsafe { LOCK.lock() };
        for c in s.bytes() {
            write_serial_with_port(self.port, c);
        }
        unsafe { LOCK.unlock() };
        Ok(())
    }
}

pub fn get_serial() -> Serial {
    Serial {
        port: SerialPort::COM1,
    }
}
